#include<queue>
#include <iostream>
using namespace std;

#define MaxN 310

int t, board, startx, starty, endx, endy;
int visited[MaxN][MaxN];
char map[MaxN][MaxN];
int dirx[] = {1,2,2,1,-1,-2,-2,-1};
int diry[] = {2,1,-1,-2,-2,-1,1,2};

struct Node
{
	int x, y, time;
	Node() :x(0), y(0), time(0) {}
	Node(int x, int y, int time) :x(x), y(y), time(time) {}
	Node(const Node & node) :x(node.x), y(node.y), time(node.time) {}
};

int IsEnd(const Node&node)
{
	return node.x == endx&&node.y == endy;
}
int sol()
{
	Node now(startx, starty, 0);
	queue<Node>q;
	q.push(now);
	visited[startx][starty] = 1;
	while (!q.empty())
	{
		now = q.front();
		q.pop();
		if (IsEnd(now))
			return now.time;
		for (int i = 0; i<8; i++)
		{
			Node next = now;
			next.x += dirx[i];
			next.y += diry[i];
			if (next.x >= 0 && next.x<board &&
				next.y >= 0 && next.y<board && 
				!visited[next.x][next.y])
			{				
				next.time = now.time + 1;
				visited[next.x][next.y] = 1;
				q.push(next);
			}
		}
	}	
	return 0;
}

int main()
{
	
	
	cin >> t;
	while (t--)
	{
		cin >> board;
		cin >> startx >> starty;
		cin >> endx >> endy;
		
		if (startx == endx&&starty == endy)
		{
			cout << 0 << endl;
			continue;
		}	

		memset(visited, 0, sizeof(visited));
		int ans = sol();
		cout << ans << endl;
	}
	return 0;
}